import QtQuick 2.15
import QtQuick.Controls 2.15
import MainworkCpp 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: "---SSH---"
    color: "#a25416"

    Mainwork {
        id: mainwork

        onJsonConfigChanged: {
            console.log("1")
            console.log(jsonConfig)
        }
    }

    GridView {
        id: gridView
        anchors.fill: parent
        cellWidth: parent.width/3
        cellHeight: 50

        property int nowIndex: 1

        model: ListModel {
            id:listModel
            ListElement { remark: "Add"; name: "example"; ip: "192.168.2.34"; passwd: "_insert new element" }
        }

        delegate: Rectangle {
            width: gridView.cellWidth
            height: gridView.cellHeight

            color: model.passwd === "_insert new element"? "red": index % 2 === 0 ? "lightgray" : "#add8e6"
            Row {
                id: row
                anchors.centerIn: parent
                Text {
                    text: model.remark + ":" + model.name + " "+ model.ip + " "
                }
            }

            MouseArea {
                anchors.fill: parent

                onEntered: {
                    parent.color = "red"
                }

                onExited: {
                    parent.color = model.passwd === "_insert new element"? "red": index % 2 === 0 ? "lightgray" : "#add8e6"
                }

                onClicked: {
                    if(model.passwd === "_insert new element") {
                        // 界面一，用于新增用户
                        newUser.visible = true
                    }
                    else {
                        // 界面二， 用于选择是否执行本模块里的内容
                        execCmd.visible = true
                        execCmd.titleText = model.name + "@" + model.ip
                        execCmd.passwd = model.passwd
                        execCmd.remark = model.remark
                        gridView.nowIndex = model.index
                    }
                }
            }
        }
        function delete_one() {
            listModel.remove(nowIndex, 1)
        }
    }

    ExecCmd {
        anchors.centerIn: parent
        id:execCmd
        visible: false

        property string passwd: "1"
        property string remark: ""

        onClick_result: {

            if(resultStr === "start") {
                mainwork.cmd_exec("terminator -e \"sshpass -p " + passwd + " ssh -o StrictHostKeyChecking=no " + titleText + "\"")
            }
            else if(resultStr === "update") {
                console.log("未实现")
            }
            else if(resultStr === "delete") {
                mainwork.remove(remark)
                gridView.delete_one()
            }
            execCmd.visible = false
        }
    }

    NewUser {
        id: newUser
        visible: false
        anchors.centerIn: parent

        onConfirm:  {
            var jsonObject = {
                name: nameStr,
                ip: ipStr,
                passwd: passwdStr
            };

            mainwork.insert(remarkStr, jsonObject)
            listModel.insert(listModel.count-1, {remark: remarkStr, name: nameStr, ip: ipStr, passwd: passwdStr})
        }
    }

    // 在 QML 中获取 QJsonObject 并解析
    Component.onCompleted: {
        var jsonObject = mainwork.getJsonConfig();

        for (var key in jsonObject) {
            var value = jsonObject[key];
            listModel.insert(listModel.count-1, {remark: key, name: value.name, ip: value.ip, passwd: value.passwd})
        }
    }

}
